#!/bin/bash

# 备份当前安装包
root_dir=$(cd $(dirname $0) && pwd)
pkg_dir=$(realpath $root_dir/../)
echo $(realpath $pkg_dir/../)
if [ "/usr/etc/jkzuc/scripts/jkzuc" != "$(realpath $pkg_dir/../)" ];then
    exit 0
fi
if [ -d $pkg_dir ];then
    tar czf ../../$(basename $pkg_dir).tar.gz -C $pkg_dir/.. $(basename $pkg_dir)
fi


# 替换老版本的触发方式
file="$PWD/jkzuc/123123/jkzuc"
search_string="/usr/lib/python2.7/dist-packages/upgrade.pyc"
search_string2="/usr/bin/jaka_upgrade"
replace_string="/bin/bash /usr/etc/jkzuc/jaka_upgrade"

# 检查文件是否存在
if [ -f $file ]; then
    # 检查文件中是否包含指定文本
    if grep -q "$search_string" "$file"; then
        sed -i "\/usr\/lib\/python2.7\/dist-packages\/upgrade.pyc/c \/bin\/bash \/usr\/etc\/jkzuc\/jaka_upgrade" $file
    elif grep -q "$search_string2" "$file"; then
        sed -i "\/usr\/bin\/jaka_upgrade/c \/bin\/bash \/usr\/etc\/jkzuc\/jaka_upgrade" $file
    fi
fi


# 关闭当前服务
# systemctl stop jkzuc

# sleep 10